import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'
import errorMag from '@/utils/error'
import {
    getCookie,
    setCookie,
    removeCookie
} from '@/utils/js-cookie'
const baseURL = 'http://localhost:3001/'

const service = axios.create({
    baseURL,
    timeout: 3000
})

service.interceptors.request.use(config => {
    return config
}, error => {
    return Promise.reject(error)
})

service.interceptors.response.use(res => {
    if (res.data.code == 200) {
        return res.data
    } else {
        ElMessageBox.confirm(
            '错误消息:' + (errorMag[res.data.code] || res.data.msg),
            '错误提示！',
            {
                confirmButtonText: 'OK',
                cancelButtonText: 'Cancel',
                showCancelButton: false,
                type: 'warning',
            }
        )
            .then(() => {
                removeCookie('token')
                location.reload()  //刷新页面
                // ElMessage({
                //     type: 'success',
                //     message: 'Delete completed',
                // })
            })
            .catch(() => {
                // ElMessage({
                //     type: 'info',
                //     message: 'Delete canceled',
                // })
            })
    }
}, error => {
    return Promise.reject(error)
})


export default service